#!/bin/bash
#…………………………………………百度AI端口初始化…………………………………………………………#
path=$(pwd)
API_KEY=$(cat ./configure/config |grep API_KEY| awk -F '=' '{print $2}')
SECRET_KEY=$(cat ./configure/config |grep SECRET_KEY| awk -F '=' '{print $2}')
client='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='$API_KEY'&client_secret='$SECRET_KEY
access_token=$(curl -i -k $client |tr "," "\n"|grep access_token| awk -F ':' '{print $2}')
export access_token
###jpg
for files in `ls -1 | grep \.jpg`
do
    folder_list[j]=$files
    j=`expr $j + 1`
        if [ ! -d result  ];then
          mkdir ./result
        fi
        if [ ! -d done  ];then
          mkdir ./done    
        fi 
    export files_pash=$path/$files
    echo $files_pash
    python3 ./configure/accurate.py  >>tmp.txt
    mv $files ./done/$files   
    echo ‘ 文件 $files 处理完成‘
    result=./tmp.txt
    cat $result |tr "}" "\n"|grep words | awk -F ':' '{print $2}' |tr -d "'" >>./result/$files\(文字\).txt 
    rm -r ./tmp.txt
done

###  png格式
for files in `ls -1 | grep \.png`
do
    folder_list[j]=$files
    j=`expr $j + 1`
        if [ ! -d result  ];then
          mkdir ./result
        fi
        if [ ! -d done  ];then
          mkdir ./done    
        fi 
    export files_pash=$path/$files
    echo $files_pash
    python3 ./configure/accurate.py  >>tmp.txt
    mv $files ./done/$files   
    echo ‘ 文件 $files 处理完成‘
    result=./tmp.txt
    cat $result |tr "}" "\n"|grep words | awk -F ':' '{print $2}' |tr -d "'" >>./result/$files\(文字\).txt 
    rm -r ./tmp.txt
done

###bmp格式
for files in `ls -1 | grep \.bmp`
do
    folder_list[j]=$files
    j=`expr $j + 1`
        if [ ! -d result  ];then
          mkdir ./result
        fi
        if [ ! -d done  ];then
          mkdir ./done    
        fi 
    export files_pash=$path/$files
    echo $files_pash
    python3 ./configure/accurate.py  >>tmp.txt
    mv $files ./done/$files   
    echo ‘ 文件 $files 处理完成‘
    result=./tmp.txt
    cat $result |tr "}" "\n"|grep words | awk -F ':' '{print $2}' |tr -d "'" >>./result/$files\(文字\).txt  
    rm -r ./tmp.txt
done

###pdf格式
for files_pdf in `ls -1 | grep \.pdf`
do
     folder_list[j]=$files_pdf
            j=`expr $j + 1`
            if [ ! -d result  ];then
              mkdir ./result
            fi
            if [ ! -d done  ];then
              mkdir ./done    
            fi
        convert $files_pdf $files_pdf.jpg       
        mv $files_pdf ./done/$files_pdf
        echo ‘ 文件 $files_pdf 已转换为jpg格式‘
      for files in `ls -1 | grep \.jpg`
        do
        folder_list[j]=$files
        j=`expr $j + 1`
            if [ ! -d result  ];then
              mkdir ./result
            fi
            if [ ! -d done  ];then
              mkdir ./done    
            fi 
        export files_pash=$path/$files
        echo $files_pash
        python3 ./configure/accurate.py  >>tmp.txt
        mv $files ./done/$files 
        done
        result=./tmp.txt
        cat $result |tr "}" "\n"|grep words | awk -F ':' '{print $2}' |tr -d "'" >>./result/$files\(文字\).txt  
        echo ‘ 文件 $files_pdf 处理完成‘
        rm -r ./tmp.txt
done

